package com.demo.service.impl;

import com.demo.mapper.ComployeeMapper;
import com.demo.mapper.InoutMapper;
import com.demo.mapper.ZcMapper;
import com.demo.pojo.Comployee;
import com.demo.pojo.Inout;
import com.demo.pojo.Inout;
import com.demo.pojo.Zc;
import com.demo.service.InoutService;
import com.demo.utils.DateUtils;
import com.demo.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author： Administrator
 * @date: 21/05/14 09:43
 * 描述：
 */
@Service
public class InoutServiceImpl implements InoutService {
    @Autowired
    private InoutMapper inoutMapper;
    @Autowired
    private ZcMapper zcMapper;
    @Autowired
    private ComployeeMapper comployeeMapper;

    @Override
    public int add(Inout inout) {
        return 0;
    }

    @Override
    public int del(String no) {
        return 0;
    }

    @Override
    public int del(Integer id) {
        return 0;
    }

    @Override
    public int update(Inout inout) {
       return 0;
    }

    @Override
    public Page<Inout> page(Inout obj, int pageNo, int pageSize) {
        return null;
    }

    @Override
    public Page<Inout> page(String content, String back, int pageNo, int pageSize) {
        Page<Inout> page = new Page<>();
        page.setPageSize(pageSize);
        int count = inoutMapper.pageCount(content, back);
        page.setPageTotalCount(count);
        int total = count / pageSize;
        if (count % pageSize > 0) {
            total++;
        }
        page.setPageTotal(total);
        page.setPageNo(pageNo);
        List<Inout> inouts = inoutMapper.pageByCond(content, back, (pageNo - 1) * pageSize, pageSize);
        page.setItems(inouts);
        return page;
    }

    @Override
    public int out(Inout inout) {
        Integer zcId = inout.getZcId();
        Zc zc = zcMapper.selectByPrimaryKey(zcId);
        if (zc == null) {
            return -1;
        }
        if (!"正常".equals(zc.getZcState())) {
            return -2;
        }
        String comployeeNo = inout.getComployeeNo();
        Comployee comployee = comployeeMapper.selectByPrimaryKey(comployeeNo);
        if (comployee == null) {
            return -3;
        }
        zc.setZcState("借出");
        if (zcMapper.updateByPrimaryKeySelective(zc) != 1) {
            return -4;
        }
        inout.setOutTime(DateUtils.format(new Date()));
        return inoutMapper.insertSelective(inout);
    }

    @Override
    public int back(Inout inout) {
        Inout select = inoutMapper.selectByPrimaryKey(inout.getInoutNo());
        if (select == null) {
            return -1;
        }
        if (select.getBackTime() != null) {
            return -2;
        }
        Integer zcId = select.getZcId();
        Zc zc = zcMapper.selectByPrimaryKey(zcId);
        if (!"借出".equals(zc.getZcState())) {
            return -3;
        }
        zc.setZcState("正常");
        if (zcMapper.updateByPrimaryKeySelective(zc) != 1) {
            return -4;
        }
        inout.setBackTime(DateUtils.format(new Date()));
        return inoutMapper.updateByPrimaryKeySelective(inout);
    }
}
